package com.cskt.itripauth.util;

import com.cskt.itripauth.config.EmailConfig;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Component
public class EmailUtil {

    @Resource
    private EmailConfig emailConfig;

    @Resource
    private JavaMailSender mailSender;

    @Resource
    private TemplateEngine templateEngine;

    @Resource
    private FreeMarkerConfigurer markerConfigurer;

    /**
     * 发送文本/简单的邮件
     *
     * @param receiverName 接收人
     * @param title        标题
     * @param content      内容
     */
    public void sendStringEmail(String receiverName, String title, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailConfig.getEmailFrom());
        message.setTo(receiverName);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 发送大文件/附件的邮件
     *
     * @param receiverName
     * @param title
     * @param content
     * @param file         文件
     */
    public void sendBigEmail(String receiverName, String title, String content, File file) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailConfig.getEmailFrom());
            helper.setTo(receiverName);
            helper.setSubject(title);
            helper.setText(content);
            FileSystemResource resource = new FileSystemResource(file);
            helper.addAttachment("附件", resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailSender.send(message);
    }

    /**
     * 发送模版邮件
     *
     * @param receiverName
     * @param title
     * @param information  模版参数名(html页面)
     */
    public void sendTemplateEmail(String receiverName, String title, String information) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailConfig.getEmailFrom());
            helper.setTo(receiverName);
            helper.setSubject(title);
            //封装模版使用的数据
            Map<String, Object> map = new HashMap<>();
            map.put("username", "明世隐");

            //1.FreeMarker
            //1-1 获取FreeMarker模版
            //Template markertemplate = markerConfigurer.getConfiguration().getTemplate(information);
            //1-2 将模版内容转为字符串类型并将参数传入
            //String markertTtml = FreeMarkerTemplateUtils.processTemplateIntoString(markertemplate, map);
            //1-3 将字符串作为邮件内容
            //helper.setText(markertTtml,true);

            //2.Thymeleaf
            //2-1 获取Thymeleaf模版
            Context context = new Context();
            context.setVariable("username", "瑶");
            //2-2 将模版内容转为字符串类型并将参数传入
            String thymeleafHtml = templateEngine.process("thymeleafTemplate", context);
            helper.setText(thymeleafHtml, true);

        } catch (Exception e) {
            e.printStackTrace();
        }

        mailSender.send(message);
    }

}